/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package _bai02_phantichphankhaibao;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * mot truong hop dac ta cho ra mot ket qua cu the
 * vi du: (K1 = a) /\ (a >= b)
 * @author Administrator
 */
public class jClass_TruongHopDacTa implements Interface_CacThanhPhanDacTa{
    private jClass_BieuThucLogic[] _CacDieuKien = new jClass_BieuThucLogic[0];;
//    private static String  _BieuThucDinhDang = "\\(\\s*" + jEnum_ChuoiDinhDang.TenBien.value()
  //          + "\\s*"+ jClass_BieuThucLogic.getBieuThucDinhDang() + "\\s*" + jEnum_ChuoiDinhDang.GiaTri.value()
    //        + "\\s*\\)" + "(\\s*/\\\\\\s*" + jClass_BieuThucLogic.getBieuThucDinhDang() + ")*";
private static String  _BieuThucDinhDang = "\\(.+\\)"+ "(\\s*/\\\\\\s*\\(.+\\))*";

    // <editor-fold defaultstate="collapsed" desc="getter and setter">
    public jClass_TruongHopDacTa()
    {
       //_CacDieuKien
    }

    /**
     * @return the _CacDieuKien
     */
    public jClass_BieuThucLogic[] getCacDieuKien() {
        return _CacDieuKien;
    }

    /**
     * @param CacDieuKien the _CacDieuKien to set
     */
    public void setCacDieuKien(jClass_BieuThucLogic[] CacDieuKien) {
        this.setCacDieuKien(CacDieuKien);
    }
    // </editor-fold>

    public Boolean jKiemTraChuoiHopDinhDang (String str_ChuoiCanKienTra)    {
        //String str_ChuoiDaBoSungDauPhanCach = str_ChuoiCanKienTra;
             //str_ChuoiDaBoSungDauPhanCach = str_ChuoiCanKienTra.replaceAll("\\)", ",\\)");
        Pattern pat=Pattern.compile(getBieuThucDinhDang());
        Matcher matcher =pat.matcher(str_ChuoiCanKienTra);
        return matcher.matches();
    }

    /**
     * @return the _BieuThucDinhDang
     */
    public static String getBieuThucDinhDang() {
        return _BieuThucDinhDang;
    }
    public void jCapNhatTuChuoi(String Str_BieuThucLogic) {
        Str_BieuThucLogic = Str_BieuThucLogic.trim();
        String CacBieuThuc[] = Str_BieuThucLogic.split("/\\\\");
        _CacDieuKien = new jClass_BieuThucLogic[CacBieuThuc.length];
        for (int i = 0; i < CacBieuThuc.length; i++){
            _CacDieuKien[i] = new jClass_BieuThucLogic();
            _CacDieuKien[i].jCapNhatTuChuoi(CacBieuThuc[i]);
        }
    }

    public String jToCCode(int DoLui) {
        String str_KetQua = jClass_BoTaoCode.jTaoCapDo(DoLui) + "if (";
        int i = 1;
        for (; i < _CacDieuKien.length; i++)
            str_KetQua += _CacDieuKien[i].jToCCode(DoLui) + " && ";
        if (str_KetQua.contains(" && "))
            str_KetQua = str_KetQua.substring(0, str_KetQua.lastIndexOf(" && "));
        else
            str_KetQua += 1;
        str_KetQua += ")\n" + jClass_BoTaoCode.jTaoCapDo(DoLui) + "{\n";
        DoLui++;
        str_KetQua += jClass_BoTaoCode.jTaoCapDo(DoLui) + _CacDieuKien[0].getDoiTuong1() + " = " + _CacDieuKien[0].getDoiTuong2();
        str_KetQua += ";\n" + jClass_BoTaoCode.jTaoCapDo(DoLui - 1) + "}";
        return str_KetQua;
    }

    public String jCSharpCode(int DoLui) {
        String str_KetQua = jClass_BoTaoCode.jTaoCapDo(DoLui) + "if (";
        int i = 1;
        for (; i < _CacDieuKien.length; i++)
            str_KetQua += _CacDieuKien[i].jToCCode(DoLui) + " && ";
        if (str_KetQua.contains(" && "))
            str_KetQua = str_KetQua.substring(0, str_KetQua.lastIndexOf(" && "));
        else
            str_KetQua += 1;
        str_KetQua += ")\n" + jClass_BoTaoCode.jTaoCapDo(DoLui) + "{\n";
        DoLui++;
        str_KetQua += jClass_BoTaoCode.jTaoCapDo(DoLui) + _CacDieuKien[0].getDoiTuong1() + " = " + _CacDieuKien[0].getDoiTuong2();
        str_KetQua += ";\n" + jClass_BoTaoCode.jTaoCapDo(DoLui - 1) + "}";
        return str_KetQua;
    }

    public String jVBNetCode(int DoLui) {
        String str_KetQua = jClass_BoTaoCode.jTaoCapDo(DoLui) + "if (";
        int i = 1;
        for (; i < _CacDieuKien.length; i++)
            str_KetQua += _CacDieuKien[i].jVBNetCode(DoLui) + " AND ";
        if (str_KetQua.contains(" AND "))
            str_KetQua = str_KetQua.substring(0, str_KetQua.lastIndexOf(" AND "));
        else
            str_KetQua += 1;
        str_KetQua += ")\n" + jClass_BoTaoCode.jTaoCapDo(DoLui) + "{\n";
        DoLui++;
        str_KetQua += jClass_BoTaoCode.jTaoCapDo(DoLui) + _CacDieuKien[0].getDoiTuong1() + " = " + _CacDieuKien[0].getDoiTuong2();
        str_KetQua += ";\n" + jClass_BoTaoCode.jTaoCapDo(DoLui - 1) + "}";
        return str_KetQua;
    }
}
